<?php 

/*
  Project: Blogmer - Open Source Blogging Software
  Type: PHP
  Desc: Contains all functions related to ADMIN Control Panel.
  Author: Yashpal
  Usage:  	Login Admin
			

  
*/
 
class Login extends Controller {

	function Login()
	{
		parent::Controller();	
		
	}
	
	
	
	
	function admin_login() 
	{
        $rules['username'] = 'trim|required|callback__check_login';
        $rules['password'] = 'trim|required';
        $this->validation->set_rules($rules);
        
		$fields['username'] = 'username';
        $fields['password'] = 'password';
        $this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{
            $query = $this->db->getwhere('users', array('Mname' => $this->input->post('username')));
            $row = $query->row();
     		
		   $newdata = array(
                   'username'  => $row->MName,
				   'first_name'  => $row->MFirstName,
				   'last_name'  => $row->MLastName,
				   'user_id'=> $row->MemberID,
                   'email'     => $row->MEmail,
				   'url'     => $row->MUrl,
				   'member_group'=>$row->MGroup,
				   'admin_session_id'=>random_string('unique'),
				   'usersession_id'=>random_string('unique'),
                   'logged_in' => TRUE,
                   'blogmer_admin_logged_in' => TRUE
               );
			
			 $this->session->set_userdata($newdata);
			 			 
			 $info['url']="/admin/";
			 $info['time']="2";
			 $info['title']="Information Page";
			 $info['header']="Information";
			 $info['message']="You have successfully logged in.";
			 
			 $this->load->view('info',$info);
	    } 
		else 
		{
				$data['title']="Admin Login - Blogmer";
				$this->load->view('admin/login',$data);
        }
    }
	
	  // Check the login information
    function _check_login($username) 
	{
        $password = dohash($this->validation->password);
        $query = $this->db->getwhere('users', array('MName'=>$username, 'MPassword'=>$password,'MGroup'=>'ac3478d69a3c81fa62e60f5c3696165a4e5e6ac4'), 1, 0);
        if ($query->num_rows() == 0) 
		{
            $this->validation->set_message('_check_login', 'Invalid login');
            return FALSE;
        }
        return TRUE;
    }
	
	// User Login
    function MemberLogin() {
       $login = $this->session->userdata('logged_in');
		if($login)
		{		 redirect('blog/','refresh'); }
		
	    $this->load->library('validation');
		$this->load->library('table');
		$this->load->helper('string');
		
        $rules['username'] = 'trim|required|callback__check_member_login';
        $rules['password'] = 'trim|required';
        $this->validation->set_rules($rules);
        
		$fields['username'] = 'username';
        $fields['password'] = 'password';
        $this->validation->set_fields($fields);
        
		if ($this->validation->run()) 
		{
            $query = $this->db->getwhere('users', array('Mname' => $this->input->post('username')));
            $row = $query->row();
           

		   $newdata = array(
                   'username'  => $row->MName,
				   'first_name'  => $row->MFirstName,
				   'last_name'  => $row->MLastName,
				   'user_id'=> $row->MemberID,
                   'email'     => $row->MEmail,
				   'url'     => $row->MUrl,
				   'usersession_id'=>random_string('unique'),
                   'logged_in' => TRUE
               );
			
			 $info['url']="/blog/";
			 $info['time']="2";
			 $info['title']="Information Page";
			 $info['header']="Information";
			 
			 if($row->Activate)
			{
			 $this->session->set_userdata($newdata);
			 $info['message']="You have successfully logged in.";
			 $this->load->view('info',$info);
			}
			else
			{ 
			// $this->session->set_userdata($newdata);
			 $info['time']="10";
			 $info['message']="You have successfully logged in. But your email is not yet verified. Please activate your account.Check your inbox and try again.";
			 $this->load->view('info',$info);
			} 
			
        } 
		else 
		{
		        $data=final_sidebar_data();
				$data['title']="Login | ".$this->session->userdata('BLOGName');
				$data['content']=$this->load->view('users/login',$data,TRUE);
				$data['where']='single';
				$this->load->view('main',$data); 
        }
    }
	
	 // Check the login information
    function _check_member_login($username) 
	{
        $this->load->helper('security');
        $password = dohash($this->validation->password);
        $query = $this->db->getwhere('users', array('MName'=>$username, 'MPassword'=>$password), 1, 0);
        if ($query->num_rows() == 0) 
		{
            $this->validation->set_message('_check_login', 'Invalid login');
            return FALSE;
        }
        return TRUE;
    }
	
}

